package com.wlc.controller;

import com.wlc.pojo.ResignationStats;
import com.wlc.pojo.Result;
import com.wlc.service.ResignationStatsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/stats/resignation")
@Slf4j
public class ResignationStatsController {

    @Autowired
    private ResignationStatsService resignationStatsService;

    @GetMapping("/dept")
    public Result<List<ResignationStats>> getDeptStats(@RequestParam(required = false) String year) {
        log.info("获取部门辞职统计: year={}", year);
        try {
            List<ResignationStats> stats = resignationStatsService.getDeptResignationStats(year);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取部门辞职统计失败: {}", e.getMessage());
            return Result.error("获取部门辞职统计失败");
        }
    }

    @GetMapping("/monthly-trend")
    public Result<List<ResignationStats>> getMonthlyTrend(@RequestParam(required = false) String year) {
        log.info("获取月度辞职趋势: year={}", year);
        try {
            List<ResignationStats> trend = resignationStatsService.getMonthlyResignationTrend(year);
            return Result.success(trend);
        } catch (Exception e) {
            log.error("获取月度辞职趋势失败: {}", e.getMessage());
            return Result.error("获取月度辞职趋势失败");
        }
    }

    @GetMapping("/reason")
    public Result<List<ResignationStats>> getReasonStats(@RequestParam(required = false) String year) {
        log.info("获取辞职原因统计: year={}", year);
        try {
            List<ResignationStats> stats = resignationStatsService.getResignReasonStats(year);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取辞职原因统计失败: {}", e.getMessage());
            return Result.error("获取辞职原因统计失败");
        }
    }

    @GetMapping("/status")
    public Result<List<ResignationStats>> getStatusStats(@RequestParam(required = false) String year) {
        log.info("获取辞职状态统计: year={}", year);
        try {
            List<ResignationStats> stats = resignationStatsService.getResignationStatusStats(year);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取辞职状态统计失败: {}", e.getMessage());
            return Result.error("获取辞职状态统计失败");
        }
    }

    @GetMapping("/duration")
    public Result<List<ResignationStats>> getDurationStats(@RequestParam(required = false) String year) {
        log.info("获取在职时长统计: year={}", year);
        try {
            List<ResignationStats> stats = resignationStatsService.getWorkDurationStats(year);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取在职时长统计失败: {}", e.getMessage());
            return Result.error("获取在职时长统计失败");
        }
    }

    @GetMapping("/summary")
    public Result<ResignationStats> getSummary(@RequestParam(required = false) String year) {
        log.info("获取辞职汇总统计: year={}", year);
        try {
            ResignationStats summary = resignationStatsService.getResignationSummary(year);
            return Result.success(summary);
        } catch (Exception e) {
            log.error("获取辞职汇总统计失败: {}", e.getMessage());
            return Result.error("获取辞职汇总统计失败");
        }
    }

    @GetMapping("/yearly-rate")
    public Result<List<ResignationStats>> getYearlyRate(@RequestParam(required = false) Integer startYear,
                                                        @RequestParam(required = false) Integer endYear) {
        log.info("获取年度辞职率趋势: startYear={}, endYear={}", startYear, endYear);
        try {
            List<ResignationStats> trend = resignationStatsService.getYearlyResignationRate(startYear, endYear);
            return Result.success(trend);
        } catch (Exception e) {
            log.error("获取年度辞职率趋势失败: {}", e.getMessage());
            return Result.error("获取年度辞职率趋势失败");
        }
    }

    @GetMapping("/overview")
    public Result<Object> getOverview(@RequestParam(required = false) String year) {
        log.info("获取辞职统计总览: year={}", year);
        try {
            Object overview = resignationStatsService.getResignationOverview(year);
            return Result.success(overview);
        } catch (Exception e) {
            log.error("获取辞职统计总览失败: {}", e.getMessage());
            return Result.error("获取辞职统计总览失败");
        }
    }
}